package com.java.netty.nio.netty.heardbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @Author: Dgg
 * @Date: 2022/1/3 21:11
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {
	/**
	 * @param ctx 上下文
	 * @param evt 事件
	 * @throws Exception
	 */
	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		if (evt instanceof IdleStateEvent) {

			//将 evt 向下转型成 IdleStateEvent
			IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
			String eventType = null;
			switch (idleStateEvent.state()) {
				case READER_IDLE:
					eventType = "读空闲";
					break;
				case WRITER_IDLE:
					eventType = "写空闲";
				case ALL_IDLE:
					eventType = "都写空闲";
			}
			System.out.println(ctx.channel().remoteAddress() + "--- 超时时间---" + eventType);
			System.out.println("服务器做相应处理");

			//如果发生空闲关闭通道
			ctx.channel().close();
		}
	}
}
